package com.jgssp.ad.sdk;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.jgssp.ad.config.TTAdManagerHolder;
import com.jgssp.ad.constant.AdConstant;
import com.jgssp.ad.utils.TToast;

public class InterstitialFullActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "JgAds" + InterstitialFullActivity.class.getSimpleName();

    private TextView mTvAdUnitId; //插全屏广告位id
    private Button mBtLoadInterFull; //加载插全屏广告
    private Button mBtShowInterFull;//展示插全屏广告
    private RadioGroup radioGroup;

    private static String mAdUnitId = AdConstant.INTERSTITIAL; //插全屏广告位

    private boolean mLoadSuccess; //是否加载成功
    private boolean mIsLoadedAndShow;//广告加载成功并展示

    private TTAdNative mTTAdNative;
    private TTFullScreenVideoAd mTtFullScreenVideoAd;

    @SuppressWarnings("RedundantCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intersitial_full_ad);
        mTvAdUnitId = findViewById(R.id.tv_ad_unit_id);
        mBtLoadInterFull = findViewById(R.id.bt_load_inter_full);
        mBtShowInterFull = findViewById(R.id.bt_show_inter_full);
        radioGroup = findViewById(R.id.radio_group);

        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);

        initRadioGroup();
        initListener();
        initAdLoader();
    }

    private void initRadioGroup() {

    }

    @Override
    public void initListener() {
        mBtLoadInterFull.setOnClickListener(this);
        mBtShowInterFull.setOnClickListener(this);
    }

    @Override
    public void initAdLoader() {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_load_inter_full: //加载插全屏广告
                mLoadSuccess = false;
                mIsLoadedAndShow = false;
                loadAd(mAdUnitId);
                break;
            case R.id.bt_show_inter_full://展示插全屏广告
                showInterFullAd();
                break;
        }
    }

    /**
     * 加载广告
     */
    private void loadAd(final String codeId) {
        //step5:创建广告请求参数AdSlot
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) // 广告代码位Id
                .setAdLoadType(TTAdLoadType.LOAD) // 本次广告用途：TTAdLoadType.LOAD实时；TTAdLoadType.PRELOAD预请求
                .build();

        //step6:注册广告加载生命周期监听，请求广告
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int code, String msg) {
                TToast.show(InterstitialFullActivity.this, msg + " code:" + code);
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {
                mTtFullScreenVideoAd = ttFullScreenVideoAd;

                if (mIsLoadedAndShow) {
                    showInterFullAd();
                }
            }

            @Override
            public void onFullScreenVideoCached() {

            }

            @Override
            public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
                mTtFullScreenVideoAd = ttFullScreenVideoAd;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 展示广告
     */
    private void showInterFullAd() {
        if (mTtFullScreenVideoAd != null) {
            mTtFullScreenVideoAd.showFullScreenVideoAd(this);
        } else {
            TToast.show(this, "请先加载广告");
        }
    }
}
